package com.liujun.spring4.demo.aop.ioc.base.privatecar;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author liujun
 * @version 1.0
 * @date 2018-08-10 22:15:00
 */
public class PrivetCarReflect {

  public static void main(String[] args) throws Exception {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    Class clazz = loader.loadClass("com.liujun.spring4.demo.aop.ioc.base.privatecar.PrivateCar");
    PrivateCar pcar = (PrivateCar) clazz.newInstance();
    // 获取属性
    Field colorField = clazz.getDeclaredField("color");

    // 1,取消java语言访问检查以访问private变量
    colorField.setAccessible(true);
    colorField.set(pcar,"red");

      Method driveMtd = clazz.getDeclaredMethod("drive",null);
      driveMtd.setAccessible(true);
      driveMtd.invoke(pcar,null);
  }
}
